<?php
/**
 * User: jc234316
 * Date: 25/05/2014
 * Time: 1:44PM
 */

require 'init.php';

if (empty($_POST) === false) {
    $name = trim($_POST['name']);
    $cardType = trim($_POST['cardType']);
    $cardNumber = trim($_POST['cardNumber']);
    $securityCode = trim($_POST['securityCode']);

    validateCC($cardNumber, $cardType);
}

// The original code for this function was obtained from http://stackoverflow.com/questions/174730/what-is-the-best-way-to-validate-a-credit-card-in-php
function validateCC($cc_num, $type)
{
    $verified = false;

    if ($type == "American Express") {
        $pattern = "/^([34|37]{2})([0-9]{13})$/"; //American Express
        if (preg_match($pattern, $cc_num)) {
            $verified = true;
        } else {
            $verified = false;
        }
    } elseif ($type == "Discover") {
        $pattern = "/^([6011]{4})([0-9]{12})$/"; //Discover Card
        if (preg_match($pattern, $cc_num)) {
            $verified = true;
        } else {
            $verified = false;
        }
    } elseif ($type == "MasterCard") {
        $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/"; //Mastercard
        if (preg_match($pattern, $cc_num)) {
            $verified = true;
        } else {
            $verified = false;
        }
    } elseif ($type == "Visa") {
        $pattern = "/^([4]{1})([0-9]{12,15})$/"; //Visa
        if (preg_match($pattern, $cc_num)) {
            $verified = true;
        } else {
            $verified = false;
        }
    }
    if ($verified == false) {
        //Do something here in case the validation fails
        $message[] = "Credit card invalid. Please make sure that you entered a valid <em>" . $type . "</em> credit card ";
    } else if (luhn_check($cc_num)) // if it is a verified number, and passes the luhn check.
    {
        header('Location: thankyou.php');
    } else {
        // If the numbers were simply a string of random numbers.
        $message[] = "Credit card invalid. Please make sure that you entered a valid <em>" . $type . "</em> credit card ";
    }
}

/* Luhn algorithm number checker - (c) 2005-2008 shaman - www.planzero.org *
 * This code has been released into the public domain, however please      *
 * give credit to the original author where possible.                      */
function luhn_check($number)
{
    // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
    $number = preg_replace('/\D/', '', $number);

    // Set the string length and parity
    $number_length = strlen($number);
    $parity = $number_length % 2;

    // Loop through each digit and do the maths
    $total = 0;
    for ($i = 0; $i < $number_length; $i++) {
        $digit = $number[$i];
        // Multiply alternate digits by two
        if ($i % 2 == $parity) {
            $digit *= 2;
            // If the sum is two digits, add them together (in effect)
            if ($digit > 9) {
                $digit -= 9;
            }
        }
        // Total up the digits
        $total += $digit;
    }
    // If the total mod 10 equals 0, the number is valid
    return ($total % 10 == 0) ? TRUE : FALSE;
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script src="javascripts/validateForm.js"></script>
    <title>Payments</title>
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <div id="contentFrame">
            <h3>Payment Options</h3>

            <div id="paymentButtons" style="float: left;">
                <input type="submit" value="Money Order" id="moneyOrderButton" onclick="showHide('moneyOrder')"/>
                <br/>
                <input type="submit" value="Direct Deposit" id="directDepositButton"
                       onclick="showHide('directDeposit')"/>
                <br/>
                <input type="submit" value="Credit Card" id="creditCardButton" onclick="showHide('creditCard')"/>
                <br/>
            </div>
            <div id="moneyOrder" style="float: right; display: none;">
                <form action="thankyou.php" method="post" name="moneyOrderForm" id="moneyOrderForm">
                    <h3>Money Order</h3>

                    <p>
                        Slow Steamer! - Please note products will only ship AFTER receipt of money order.<br/>
                    </p>

                    <p>
                        Mr Steamy F. Tee
                    </p>

                    <p>
                        Manager<br/>
                        Steamy Tees<br/>
                        Steampunk Emporium<br/>
                        Level 7 22 Steamy St<br/>
                    </p>

                    <p>
                        CAIRNS QLD 5465
                    </p>

                    <div id="submission">
                        <input type="submit" value="COMPLETE ORDER" id="submitForm"/>
                    </div>
                </form>
            </div>
            <div id="directDeposit" style="float: right; display: none;">
                <form action="thankyou.php" method="post" name="directDepositForm" id="directDepositForm">
                    <h3>Our Bank Details.</h3>
                    <br/>
                    Account Name: Steamy Tees<br/> BSB: 058 978<br/> ACC: 125 633 255<br/>

                    <div id="submission">
                        <input type="submit" value="COMPLETE ORDER" id="submitForm"/>
                    </div>
                </form>
            </div>
            <div id="creditCard" style="float: right; display: none;">
                <form action="payments.php" method="post" name="creditCardForm" id="creditCardForm">
                    <h3>Credit Card</h3>
                    <br/>
                    <label for="name">Name on card*:</label>
                    <input type="text" name="name" id="name" size="43"
                           value="<?php if (isset($_POST['name'])) echo htmlentities($_POST['name']); ?>"
                    <br/>
                    <br/>
                    <select id="cardType" name="cardType">
                        <option
                            value="<?php if (isset($_POST['cardType'])) echo htmlentities($_POST['cardType']); ?>"></option>
                        <option value="MasterCard">MasterCard</option>
                        <option value="Visa">Visa</option>
                        <option value="American Express">American Express</option>
                        <option value="Discover">Discover</option>
                    </select>
                    <label for="cardNumber">Card Number*:</label>
                    <input type="text" name="cardNumber" id="cardNumber" size="20"
                           value="<?php if (isset($_POST['cardNumber'])) echo htmlentities($_POST['cardNumber']); ?>"
                    <br/>
                    <br/>
                    <label for="securityCode">Security Code*:</label>
                    <input type="text" name="securityCode" id="securityCode" size="1" maxlength="3"
                           value="<?php if (isset($_POST['securityCode'])) echo htmlentities($_POST['securityCode']); ?>"
                    <br/>
                    <br/>

                    <div id="submission">
                        <input type="submit" value="COMPLETE ORDER" id="submitForm"/>
                    </div>
                    <div id="statusBar">
                        <?php
                        if (empty($message) === false) {
                            echo "<script type='text/javascript'>document.getElementById('statusBar').style.display = 'block';</script>";
                            echo '<p>' . implode('<p></p>', $message) . '</p>';
                        }
                        ?>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <?php require 'footer.php' ?>
</div>
<script type="text/javascript">
    function showHide(visibleTab) {
        if (visibleTab == 'moneyOrder') {
            document.getElementById("moneyOrder").style.display = "";
            document.getElementById("directDeposit").style.display = "none";
            document.getElementById("creditCard").style.display = "none";
        } else if (visibleTab == 'directDeposit') {
            document.getElementById("moneyOrder").style.display = "none";
            document.getElementById("directDeposit").style.display = "";
            document.getElementById("creditCard").style.display = "none";
        } else {
            document.getElementById("moneyOrder").style.display = "none";
            document.getElementById("directDeposit").style.display = "none";
            document.getElementById("creditCard").style.display = "";
        }
    }
</script>
</body>
</html>
